package gui.reports.removed;

import java.io.File;
import java.util.Date;

import gui.common.*;
import hit.core.HomeInventoryTracker;
import hit.dao.DataAccessObject;
import hit.dao.FactoryReference;
import hit.reports.HTMLBuilder;
import hit.reports.PDFBuilder;
import hit.reports.ProductStatisticsReport;
import hit.reports.RemovedItemsReport;
import hit.reports.ReportBuilder;

/**
 * Controller class for the removed items report view.
 */
public class RemovedReportController extends Controller implements
		IRemovedReportController {

	/**
	 * Constructor.
	 * 
	 * @param view Reference to the removed items report view
	 */
	public RemovedReportController(IView view) {
		super(view);

		construct();
	}

	//
	// Controller overrides
	//

	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IRemovedReportView getView() {
		return (IRemovedReportView) super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() {
		HomeInventoryTracker hitCore = HomeInventoryTracker.Instance();
		
		Date lastRun = hitCore.GetRemovedReportLastRun();
		
		if (lastRun == null)
		{
			getView().setSinceLast(false);
			getView().setSinceDate(true);
			getView().enableSinceLast(false);
		}
		else
		{
			getView().setSinceLast(true);
			getView().setSinceDate(false);
			getView().enableSinceDateValue(false);
			getView().setSinceLastValue(lastRun);
		}
		
		getView().setSinceDateValue(new Date());
	}

	//
	// IExpiredReportController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * removed items report view is changed by the user.
	 */
	@Override
	public void valuesChanged() {
		if (getView().getSinceLast()) {
			getView().enableSinceDateValue(false);
		}
		else {
			getView().enableSinceDateValue(true);
		}
	}

	/**
	 * This method is called when the user clicks the "OK"
	 * button in the removed items report view.
	 */
	@Override
	public void display() {
		
		try
		{			
			Date runDate;
			HomeInventoryTracker hitCore = HomeInventoryTracker.Instance();
			
			if (getView().getSinceLast())
			{
				runDate = hitCore.GetRemovedReportLastRun();
			}
			else
			{
				runDate = getView().getSinceDateValue();
			}
			
			ReportBuilder builder;
			RemovedItemsReport designer = new RemovedItemsReport(runDate);
			
			FileFormat format = getView().getFormat();
			Date date = new Date();
			String filename = ReportBuilder.PATH + "removed-" + date.getTime(); 
			
			if (format == FileFormat.HTML)
			{
				builder = new HTMLBuilder();
				filename += ".html";
			}
			else if (format == FileFormat.PDF)
			{
				builder = new PDFBuilder();
				filename += ".pdf";
			}
			else
			{
				getView().displayErrorMessage("Unknown Format type.");
				return;
			}
			
			hitCore.GetProductIndex().Accept(designer);
			hitCore.GetItemIndex().Accept(designer);
			
			designer.BuildReport(builder);
			
			(new File(ReportBuilder.PATH)).mkdirs();
			File file = new File(filename);
			
			if (format == FileFormat.HTML)
			{
				((HTMLBuilder)builder).GetOutput(file);
			}
			else if (format == FileFormat.PDF)
			{
				((PDFBuilder)builder).GetOutput(file);
			}
			
			java.awt.Desktop.getDesktop().open(file);
			
			hitCore.SetRemovedReportLastRun(new Date());
			
			DataAccessObject dao = FactoryReference.GetFactory().CreateDataAccessObject();			
			dao.StartTransaction();
			
			dao.GetSettingsDAO().Update(HomeInventoryTracker.Instance());
			dao.EndTransaction(true);
			dao.CloseConnection();
		}
		catch(Exception ex)
		{
			getView().displayErrorMessage("Error attempting to create report.");
		}
	}
}

